package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/destination")
public class DestinationController {
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IRegionService regionService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    //查询热门目的地
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IMonthCommendService monthCommendService;
    @Reference
    private IStrategyThemeService strategyThemeService;
    @Reference
    private IThemeCommendService themeCommendService;
    @RequestMapping("")
    public String index(Model model) {
        List<ThemeCommend> tcs = themeCommendService.list();
        model.addAttribute("tcs",tcs);
        model.addAttribute("hotRegions", regionService.queryHotRegions());
        List<Destination> list = destinationService.list();
        int index = new Random().nextInt(list.size()-5);
        list = list.subList(index,index+5);
        model.addAttribute("dests",list);
        return "destination/index";
    }
    //共享月份数据
    @RequestMapping("/monthCommend")
    public String monthCommend(Model model){
        List<MonthCommend> mcs = new ArrayList<>();
        for (int i = 1;i<=12;i++){
            MonthCommend mc = monthCommendService.selectListForMonth(i);
            mcs.add(mc);
        }
        model.addAttribute("mcs",mcs);
        return "/destination/monthCommendTpl";
    }
    //共享详细主题数据
    @RequestMapping("/strategyTheme")
    public String strategyTheme(Model model){
        List<List<StrategyTheme>> bigList = new ArrayList<>();
        List<ThemeCommend> list = themeCommendService.list();
        for(ThemeCommend tc:list){

            List<StrategyTheme> list1 = strategyThemeService.selectByBigTheme(tc.getId());
            bigList.add(list1);
        }

        model.addAttribute("bigList",bigList);
        return "destination/strategyDetailCommendTpl";
    }
    //根据区域查询热门地区
    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Model model, Long regionId) {
        //根据热门地区查询区域,在对半分拼接到页面上
        List<Destination> destinations = destinationService.getDestByRegionId(regionId);
        //把上面查询的所有对半拆
        List<Destination> leftDests = destinations.subList(0, destinations.size() / 2 + 1);
        List<Destination> rightDests = destinations.subList(destinations.size() / 2 + 1, destinations.size());
        model.addAttribute("rightDests", rightDests);
        model.addAttribute("leftDests", leftDests);
        model.addAttribute("regionId", regionId);
        return "destination/hotdestTpl";
    }

    //面包屑
    @RequestMapping("/guide")
    public String guide(Model model, Long id) {
        toast(model, id);
        //catalogs 查询当前目的地下的所有攻略分类,同事关联查询各个分类下的所有攻略
        List<StrategyCatalog> catalogs = strategyCatalogService.getCatalogsByDestId(id);
        model.addAttribute("catalogs", catalogs);
        //查询当前目的下所有的关联攻略明细， 显示点击量最高3篇(这里可以自定义，比如：回复数最高)
        List<StrategyDetail> strategyDetails = strategyDetailService.getCatalogsViemnumTop3(id);
        model.addAttribute("strategyDetails", strategyDetails);
        return "destination/guide";
    }


    @RequestMapping("/surveyPage")
    //Long destId,Long catalogId
    public String surveyPage(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo) {
        //toasts
        toast(model, qo.getDestId());
        return "destination/survey";
    }

    @RequestMapping("/survey")
    public String survey(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo) {
        //catalogs  catalog detail
        //catalogs(左边要显示的效果) 查询当前目的地下的所有攻略分类,同事关联查询各个分类下的所有攻略
        List<StrategyCatalog> catalogs = strategyCatalogService.getCatalogsByDestId(qo.getDestId());
        model.addAttribute("catalogs", catalogs);
        if (catalogs.size() > 0) {
            //catalog表示被选中下的显示出来,当前攻略分类下的第一个明细显示出来  内容查出来
            for (StrategyCatalog catalog : catalogs) {
                if (qo.getCatalogId() == catalog.getId()) {
                    model.addAttribute("catalog", catalog);
                    List<StrategyDetail> details = catalog.getDetails();
                    if (details.size() > 0) {
                        StrategyDetail detail = details.get(0);
                        detail.setStrategyContent(strategyDetailService.getContent(detail.getId()));
                        model.addAttribute("detail", detail);
                    }
                }
            }
        }
        return "destination/surveyTpl";
    }

    private void toast(Model model, Long id) {
        List<Destination> toasts = destinationService.getToasts(id);
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", dest);
    }

    @RequestMapping("/travels")
    public String travels(Model model, @ModelAttribute("qo") TravelQuery qo) {
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "destination/travelTpl";
    }
    @RequestMapping("/search")
    public String search(Model model,String dest){
        List<Destination> destinations = destinationService.selectDestBySearch(dest);
        model.addAttribute("dests",destinations);
        return "destination/searchDestTpl";
    }
}
